<?php

declare (strict_types = 1);

namespace app\common\service\user;

use app\common\service\BaseService;
use app\common\exception\AdminException;
use app\common\repository\user\UserTokensRepository;

class UserTokensService extends BaseService
{
    public function __construct(UserTokensRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取列表
     *
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getList($where = []): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        unset($where['page'], $where['page_size']);
        return $this->repository->getTagList($where, $page, $pageSize);
    }

    /**
     * 获取详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id)
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建信息
     *
     * @param array $data
     * @return \app\common\model\BaseModel|array|\think\Collection
     * @throws \Exception
     */
    public function create(array $data): \think\Collection|\app\common\model\BaseModel|array
    {
        return $this->repository->save($data);
    }

    /**
     * 更新信息
     *
     * @param array $data
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $data, $id)
    {
        if(empty($id)) {
            throw new AdminException('ID 不能为空');
        }
        return $this->repository->update($data, $id);
    }

    /**
     * 删除信息
     *
     * @param $id
     * @return bool
     */
    public function delete($id)
    {
        return $this->repository->delete($id);
    }
}